package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.dao.CustomerReturnListDao;
import com.atguigu.jxc.dao.CustomerReturnListGoodsDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.entity.CustomerReturnList;
import com.atguigu.jxc.entity.CustomerReturnListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerReturnListGoodsServiceImpl implements CustomerReturnListGoodsService {

    @Autowired
    private CustomerReturnListGoodsDao customerReturnListGoodsDao;

    @Autowired
    private CustomerDao customerDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private CustomerReturnListDao customerReturnListDao;


    @Override
    public ServiceVO save(CustomerReturnList customerReturnList, String customerReturnListGoodsStr, String returnNumber) {
        CustomerReturnList customerReturnListVo = new CustomerReturnList();
        customerReturnListVo.setReturnNumber(returnNumber);
        customerReturnListVo.setAmountPaid(customerReturnList.getAmountPaid());
        customerReturnListVo.setAmountPayable(customerReturnList.getAmountPayable());
        customerReturnListVo.setReturnDate(customerReturnList.getReturnDate());
        customerReturnListVo.setRemarks(customerReturnList.getRemarks());
        customerReturnListVo.setState(customerReturnList.getState());
        customerReturnListVo.setCustomerId(customerReturnList.getCustomerId());
        customerReturnListVo.setUserId(1);// TODO 直接给了，不知道怎么获取

        Customer customer= customerDao.getCustomerById(customerReturnList.getCustomerId());
        customerReturnListVo.setCustomerName(customer.getCustomerName());

        User userById = userDao.getUserById(customerReturnListVo.getUserId());//TODO
        customerReturnListVo.setTrueName(userById.getTrueName());
        customerReturnListDao.save(customerReturnListVo);

        Gson gson = new Gson();
        CustomerReturnListGoods[] customerReturnListGoods = gson.fromJson(customerReturnListGoodsStr, CustomerReturnListGoods[].class);

        for (CustomerReturnListGoods customerReturnListGood : customerReturnListGoods) {
            CustomerReturnListGoods customerReturnListGoodVo = new CustomerReturnListGoods();
            customerReturnListGoodVo.setGoodsId(customerReturnListGood.getGoodsId());
            customerReturnListGoodVo.setGoodsCode(customerReturnListGood.getGoodsCode());
            customerReturnListGoodVo.setGoodsName(customerReturnListGood.getGoodsName());
            customerReturnListGoodVo.setGoodsModel(customerReturnListGood.getGoodsModel());
            customerReturnListGoodVo.setGoodsUnit(customerReturnListGood.getGoodsUnit());
            customerReturnListGoodVo.setGoodsNum(customerReturnListGood.getGoodsNum());
            customerReturnListGoodVo.setPrice(customerReturnListGood.getPrice());
            customerReturnListGoodVo.setTotal(customerReturnListGood.getTotal());
            customerReturnListGoodVo.setCustomerReturnListId(customerReturnListVo.getCustomerReturnListId());
            customerReturnListGoodVo.setGoodsTypeId(customerReturnListGood.getGoodsTypeId());
            customerReturnListGoodsDao.save(customerReturnListGoodVo);
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    @Override
    public List<CustomerReturnList> getList(String returnNumber, Integer customerId, Integer state, String sTime, String eTime) {
        List<CustomerReturnList> lists = customerReturnListDao.getList(returnNumber, customerId, state, sTime, eTime);
        for (CustomerReturnList list : lists) {
            Integer customerIdVo = list.getCustomerId();
            Customer customerById = customerDao.getCustomerById(customerIdVo);
            list.setCustomerName(customerById.getCustomerName());
            Integer userId = list.getUserId();
            User userById = userDao.getUserById(userId);
            list.setTrueName(userById.getTrueName());
        }
        return lists;
    }

    @Override
    public List<CustomerReturnListGoods> goodsList(Integer customerReturnListId) {
        return customerReturnListGoodsDao.goodsList(customerReturnListId);
    }

    @Override
    public ServiceVO delete(Integer customerReturnListId) {

        //先判断有没有详情
        List<CustomerReturnListGoods> goodsList = customerReturnListGoodsDao.goodsList(customerReturnListId);
        if (goodsList!=null){//详情有数据，循环删除
            for (CustomerReturnListGoods customerReturnListGoods : goodsList) {
                Integer customerReturnListGoodsId = customerReturnListGoods.getCustomerReturnListGoodsId();
                customerReturnListGoodsDao.deleteById(customerReturnListGoodsId);
            }
        }
        //走到这，该returnListId在详情表一定是没有数据的
        customerReturnListDao.deleteById(customerReturnListId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }
}
